﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace GetCarInfo
{
    public class carInfoService
    {
        //添加一条记录
        public static void Save(List<CarInfo> carinfos)
        {
            //Debug.Assert(carinfos == null, "carinfo不能为空");
            using (CarInfoEntitiesyin ce = new CarInfoEntitiesyin())
            {
                //for (int i = 0; i < carinfos.Count; i++)
                foreach (var car in carinfos)
                {
                    var e = ce.CarInfoes.FirstOrDefault(p => p.car_NameA == car.car_NameA
                                        && p.car_NameB == car.car_NameB
                                        && p.car_NameC == car.car_NameC
                                        && p.car_NameD == car.car_NameD);
                    if (e == null)
                    {
                        ce.CarInfoes.AddObject(car);
                    }
                    else
                    {
                        e.car_DateTime = car.car_DateTime;
                        e.car_Picture = car.car_Picture;
                        e.car_Price = car.car_Price;
                    }

                }
                ce.SaveChanges();
            }
        }

        /// <summary>
        /// 查询记录
        /// </summary>
        /// <returns></returns>
        ////无条件查询
        //public static IEnumerable<CarInfo> GetCarList()
        //{
        //    using (CarInfoEntitiesyin ce = new CarInfoEntitiesyin())
        //    {
        //        return ce.CarInfoes.ToArray();
        //    }
        //}
        //根据条件查询
        public static IEnumerable<CarInfo> GetCarList(string carType = null, decimal begin = 0.0m, decimal end = 0.0m)
        {
            using (CarInfoEntitiesyin ce = new CarInfoEntitiesyin())
            {
                if (carType == null && begin == 0.0m && end == 0.0m)
                {
                    return ce.CarInfoes.ToArray();
                }
                else
                {
                    //根据车类查询
                    if (begin == 0.0m && end == 0.0m)
                    {
                        return ce.CarInfoes.Where(p => p.car_NameA.Contains(carType)).ToArray();
                    }
                    //根据价位查询
                    if (carType == null)
                    {
                        if (carType == null && begin != 0.0m && end == 0.0m)
                        {
                            return ce.CarInfoes.Where(p => p.car_Price >= begin).ToArray();
                        }
                        else
                        {
                            //var source = from car in ce.CarInfoes 
                            //             where car.car_Price > begin && car.car_Price < end 
                            //             select car;
                            //return source.ToArray();
                            return ce.CarInfoes.Where(p => p.car_Price >= begin && p.car_Price <= end).ToArray();
                        }
                    }
                    //根据车类和价位查询
                    else
                    {
                        if (begin != 0.0m && end == 0.0m)
                        {
                            return ce.CarInfoes.Where(p => p.car_Price >= begin && p.car_NameA.Contains(carType)).ToArray();
                        }
                        //var source = from car in ce.CarInfoes 
                        //             where car.car_Price > begin && car.car_Price < end && car.car_NameA.Contains(carType) 
                        //             select car;
                        //return source.ToArray();
                        if (begin == 0.0m && end != 0.0m)
                        {
                            return ce.CarInfoes.Where(p => p.car_Price <= end && p.car_NameA.Contains(carType)).ToArray();
                        }
                        else
                        {
                            return ce.CarInfoes.Where(p => p.car_Price >= begin && p.car_Price <= end && p.car_NameA.Contains(carType)).ToArray();
                        }
                    }
                }
            }
        }
        //查询所有的车类
        public static string[] GetAllcartype()
        {
            string[] cartype = new string[100];
            using (CarInfoEntitiesyin c = new CarInfoEntitiesyin())
            {
                IList<CarInfo> list = c.CarInfoes.ToArray();
                int cartypeIndex = 0;
                for (int i = 0; i < list.Count; i++)
                {
                    CarInfo cis = (CarInfo)list[i];
                    //判断类型是否已经存在
                    //for (int j = 0; j < cartype.Length; j++)
                    //{
                    //    if (cartype[j] != cis.car_NameA)
                    //    {
                    //        cartype[i] = cis.car_NameA;
                    //    }
                    //}
                    if (Array.IndexOf(cartype, cis.car_NameA) == -1)
                    {
                        cartype[cartypeIndex++] = cis.car_NameA.ToString();
                    }
                }
                return cartype;
            }
        }
    }
}

